<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/5/24
 * Time: 下午3:56
 */

namespace App\Foundation\Traits;

use App\Constants\ErrorCode;
use App\Foundation\Utils\Aes;
use App\Foundation\Utils\Rsa;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Utils\Context;


trait CryptTrait
{
    use ResultTrait;

    //客户端传输参数解密
    public function getDecryptData($appId,RequestInterface $request){
        $secretVerify = $this->secretVerify();
        if($secretVerify){
            $data = $request->input("data");
            $aesEncryptKey = $request->input('token');

            $rsa = new Rsa();
            $aesKey = $rsa->decode($aesEncryptKey,$appId);

            if(!$aesKey){
                return ErrorCode::DECRPYPT_ERR;
              //return  $this->returncode(300,'数据校验不通过');
            }

            $iv = $this->getDecryptIv($appId,$aesKey);

            $aes = new Aes($aesKey,$iv);
            $decryptData = $aes->decrypt($data);
            if(!$decryptData){
                return ErrorCode::DECRPYPT_ERR;
                //$this->returncode(300,'数据校验不通过');
            }
            $sign = $decryptData['sign'];
            unset($decryptData['sign']);
            ksort($decryptData);
            $signArr = array_values($decryptData);

            $signStr = implode($signArr);
            if(!$rsa->checkSign($signStr,$sign,$appId)){
                return ErrorCode::DECRPYPT_ERR;
                //$this->returncode(300,'签名校验失败');
            }
        }else{
            $decryptData = $request->all() ;
        }
        return $decryptData;
    }


    //参数加密偏移量
    protected function getDecryptIv($appId,$aesKey){
        $fcName = 'getDecryptIv'.$appId;
        $methodExists = method_exists($this,$fcName);
        if($methodExists){
            $iv = $this->$fcName($aesKey);
        }else{
            $ivBefore = substr($aesKey,0,9);
            $ivBefore = strrev($ivBefore);
            $ivAfter = substr($aesKey,9);
            $ivAfter = strrev($ivAfter);
            $iv = $ivAfter.$ivBefore;
            $iv = str_replace('6','e',$iv);
        }
        return $iv;
    }

    //返回客户端加密数据
    public function getEncryptData($data,$appId){
        $aesKey = "rO7L8UJrApvExHPG";
        $rsaServer = new Rsa();
        $aesEncryptKey = $rsaServer->encrypt($aesKey,$appId);
        $aesServer = new Aes($aesKey,$aesKey);
        $dataJson = json_encode($data,JSON_UNESCAPED_UNICODE);
        $data = $aesServer->encrypt($dataJson);
        $result['server_token'] = $aesEncryptKey;
        $result['data'] = $data;
        return $result;
    }

    //参数是否加密
    protected function secretVerify(){
        $secretVerify = true;
        return $secretVerify;
//        $notSecretCon = [
//            'Test'
//        ];
//        $notSecretAct = [
//            'Index/update'
//        ];
//        if(in_array($this->request->controller(),$notSecretCon) || in_array($this->action,$notSecretAct) ){
//            $secretVerify = false;
//        }else if(false !== strpos($_SERVER['HTTP_HOST'],'api150')){
//            $secretVerify = false;
//        }
//        return $secretVerify;
    }
}